package uk.co.droidinactu.sony.prst1.home;

import java.util.List;

import uk.co.droidinactu.sony.prst1.home.data.SonyDataModel;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public final class EditPreferences extends PreferenceActivity {

    public static final String PREF_COLLECTION1_NAME = "Collection1Name";
    public static final String PREF_COLLECTION2_NAME = "Collection2Name";

    public static final String PREF_CURRENT_READ_NUMBER = "NumberRecentReadBooks";
    public static final String PREF_CURRENT_READ_NUMBER_DEF = "10";

    public static final String PREF_FIRST_EXECUTION = "FirstExecution";
    public static final String PREF_FIRST_EXECUTION_DEF = "true";

    public static final String PREF_RECENTLY_ADDED_NUMBER = "NumberRecentlyAddedBooks";
    public static final String PREF_RECENTLY_ADDED_NUMBER_DEF = "10";

    public static final String PREF_USE_ALTERNATE_LAYOUT = "UseAlternateScreenLayout";
    public static final Boolean PREF_USE_ALTERNATE_LAYOUT_DEF = false;

    public static final String PREF_USE_FILENAMES = "UseFilenames";
    public static final Boolean PREF_USE_FILENAMES_DEF = false;

    public static String getCollectionName1(final Context cntxt) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final String prefVal = mPrefs.getString(EditPreferences.PREF_COLLECTION1_NAME,
                cntxt.getResources().getString(R.string.pref_collection_periodicals));
        return prefVal;
    }

    public static String getCollectionName2(final Context cntxt) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final String prefVal = mPrefs.getString(EditPreferences.PREF_COLLECTION2_NAME,
                cntxt.getResources().getString(R.string.pref_collection_recently_added));
        return prefVal;
    }

    public static int getCurrentReadDuration(final Context cntxt) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final String prefVal = mPrefs.getString(EditPreferences.PREF_CURRENT_READ_NUMBER,
                EditPreferences.PREF_CURRENT_READ_NUMBER_DEF);
        return Integer.parseInt(prefVal);
    }

    public static int getRecentlyAddedDuration(final Context cntxt) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final String prefVal = mPrefs.getString(EditPreferences.PREF_CURRENT_READ_NUMBER,
                EditPreferences.PREF_CURRENT_READ_NUMBER_DEF);
        return Integer.parseInt(prefVal);
    }

    public static boolean isFirstExecution(final Context cntxt) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final String prefVal = mPrefs.getString(EditPreferences.PREF_FIRST_EXECUTION,
                EditPreferences.PREF_FIRST_EXECUTION_DEF);
        return prefVal.equals(EditPreferences.PREF_FIRST_EXECUTION_DEF);
    }

    public static void setCollectionName1(final Context cntxt, final String val) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final SharedPreferences.Editor ed = mPrefs.edit();
        ed.putString(EditPreferences.PREF_COLLECTION1_NAME, val);
        ed.commit();
    }

    public static void setCollectionName2(final Context cntxt, final String val) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final SharedPreferences.Editor ed = mPrefs.edit();
        ed.putString(EditPreferences.PREF_COLLECTION2_NAME, val);
        ed.commit();
    }

    public static void setCurrentReadDuration(final Context cntxt, final String val) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final SharedPreferences.Editor ed = mPrefs.edit();
        ed.putString(EditPreferences.PREF_CURRENT_READ_NUMBER, val);
        ed.commit();
    }

    public static void setFirstExecution(final Context cntxt, final boolean val) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final SharedPreferences.Editor ed = mPrefs.edit();
        ed.putString(EditPreferences.PREF_FIRST_EXECUTION, val ? EditPreferences.PREF_FIRST_EXECUTION_DEF : "false");
        ed.commit();
    }

    public static void setRecentlyAddedDuration(final Context cntxt, final String val) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final SharedPreferences.Editor ed = mPrefs.edit();
        ed.putString(EditPreferences.PREF_RECENTLY_ADDED_NUMBER, val);
        ed.commit();
    }

    public static boolean useAlternateLayout(final Context cntxt) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final boolean prefVal = mPrefs.getBoolean(EditPreferences.PREF_USE_ALTERNATE_LAYOUT,
                PREF_USE_ALTERNATE_LAYOUT_DEF);
        return prefVal;
    }

    public static boolean useFilenames(final Context cntxt) {
        final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(cntxt);
        final boolean prefVal = mPrefs.getBoolean(EditPreferences.PREF_USE_FILENAMES, PREF_USE_FILENAMES_DEF);
        return prefVal;
    }

    private ListPreference collection1Name;
    private ListPreference collection2Name;
    private EditTextPreference currentlyReading;
    private SonyDataModel dataModel;
    private EditTextPreference recentlyAdded;

    private final Preference.OnPreferenceChangeListener syncToggle = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
            boolean retValue = false;
            if (preference.getKey().equals(EditPreferences.PREF_RECENTLY_ADDED_NUMBER)) {
                EditPreferences.setRecentlyAddedDuration(EditPreferences.this, newValue.toString());
                EditPreferences.this.currentlyReading.setSummary(String.format(EditPreferences.this.getResources()
                        .getString(R.string.pref_number_of_recently_added_summary), newValue));
                retValue = true;
            } else if (preference.getKey().equals(EditPreferences.PREF_CURRENT_READ_NUMBER)) {
                EditPreferences.setCurrentReadDuration(EditPreferences.this, newValue.toString());
                EditPreferences.this.currentlyReading.setSummary(String.format(EditPreferences.this.getResources()
                        .getString(R.string.pref_number_of_recently_read_summary), newValue));
                retValue = true;
            } else if (preference.getKey().equals(EditPreferences.PREF_COLLECTION1_NAME)) {
                EditPreferences.setCollectionName1(EditPreferences.this, newValue.toString());
                EditPreferences.this.collection1Name.setSummary(String.format(EditPreferences.this.getResources()
                        .getString(R.string.pref_collection1_name_summary), newValue));
                retValue = true;
            } else if (preference.getKey().equals(EditPreferences.PREF_COLLECTION2_NAME)) {
                EditPreferences.setCollectionName2(EditPreferences.this, newValue.toString());
                EditPreferences.this.collection2Name.setSummary(String.format(EditPreferences.this.getResources()
                        .getString(R.string.pref_collection2_name_summary), newValue));
                retValue = true;
            }
            return retValue;
        }
    };

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (this.dataModel == null) {
            this.dataModel = new SonyDataModel(this);
        }

        final List<String> collectionNames = this.dataModel.getCollectionNames();
        final String[] clcntNameArryTmp = collectionNames.toArray(new String[collectionNames.size()]);
        final String[] clcntNameArry = new String[clcntNameArryTmp.length + 2];

        System.arraycopy(clcntNameArryTmp, 0, clcntNameArry, 2, clcntNameArryTmp.length);
        clcntNameArry[0] = this.getResources().getString(R.string.pref_collection_periodicals);
        clcntNameArry[1] = this.getResources().getString(R.string.pref_collection_recently_added);

        this.collection1Name = (ListPreference) this.findPreference(EditPreferences.PREF_COLLECTION1_NAME);
        this.collection1Name.setOnPreferenceChangeListener(this.syncToggle);
        this.collection1Name.setEntries(clcntNameArry);
        this.collection1Name.setEntryValues(clcntNameArry);
        String tmpValStr = EditPreferences.getCollectionName1(this);
        this.collection1Name.setSummary(String.format(
                this.getResources().getString(R.string.pref_collection1_name_summary), tmpValStr));

        this.collection2Name = (ListPreference) this.findPreference(EditPreferences.PREF_COLLECTION2_NAME);
        this.collection2Name.setOnPreferenceChangeListener(this.syncToggle);
        this.collection2Name.setEntries(clcntNameArry);
        this.collection2Name.setEntryValues(clcntNameArry);
        tmpValStr = EditPreferences.getCollectionName2(this);
        this.collection2Name.setSummary(String.format(
                this.getResources().getString(R.string.pref_collection2_name_summary), tmpValStr));

        this.currentlyReading = (EditTextPreference) this.findPreference(EditPreferences.PREF_CURRENT_READ_NUMBER);
        this.currentlyReading.setOnPreferenceChangeListener(this.syncToggle);
        int tmpValInt = EditPreferences.getCurrentReadDuration(this);
        this.currentlyReading.setSummary(String.format(
                this.getResources().getString(R.string.pref_number_of_recently_read_summary), tmpValInt));

        this.recentlyAdded = (EditTextPreference) this.findPreference(EditPreferences.PREF_RECENTLY_ADDED_NUMBER);
        this.recentlyAdded.setOnPreferenceChangeListener(this.syncToggle);
        tmpValInt = EditPreferences.getCurrentReadDuration(this);
        this.recentlyAdded.setSummary(String.format(
                this.getResources().getString(R.string.pref_number_of_recently_added_summary), tmpValInt));
    }

}
